from datetime import date, datetime
from typing import List, Union
from fastapi import Query
from pydantic import BaseModel, Field, field_validator, ValidationError
from typing_extensions import Literal
from forward.schema_base import EmptyStrToNone
from forward.utils.tools import ToolsUtil


class SystemLoginInSchema(BaseModel):
    """系统登录参数"""

    username: str = Field(min_length=2, max_length=20)  # 账号
    password: str = Field(min_length=6, max_length=32)  # 密码

    # @field_validator("password", mode="before")
    # def password_v(cls, v):
    #     print("password_v", v)
    #     if len(v) < 6 or len(v) > 32:
    #         raise ValidationError(
    #             (
    #                 [
    #                     {
    #                         "loc": ("password",),
    #                         "msg": "密码长度必须在6-32位之间",
    #                         "type": "value_error",
    #                     }
    #                 ]
    #             )
    #         )
    #     return v


class SystemLoginOutSchema(BaseModel):
    """系统登录返回信息"""

    token: str  # 令牌


class SystemLogoutInSchema(BaseModel):
    """退出登录参数"""

    token: str  # 令牌


class SystemAuthAdminSchema(BaseModel):
    """管理员模型"""

    id: int  # 主键
    username: str  # 账号
    nickname: str  # 昵称
    avatar: str  # 头像
    role_ids: Union[List[int], str] = Field(alias="role_ids")  # 角色
    dept_ids: Union[List[int], str] = Field(alias="dept_ids")  # 部门
    post_ids: Union[List[int], str] = Field(alias="post_ids")  # 岗位
    is_multipoint: int = Field(alias="is_multipoint")  # 多端登录: [0=否, 1=是]
    is_disable: int = Field(alias="is_disable")  # 是否禁用: [0=否, 1=是]
    last_login_ip: str = Field(alias="last_login_ip")  # 最后登录IP
    last_login_time: datetime = Field(alias="last_login_time")  # 最后登录时间
    created_time: datetime | None = Field(None)  # 创建时间
    updated_time: datetime | None = Field(None)  # 更新时间
    is_delete: int
    salt: str
    password: str


class SystemAuthAdminListInSchema(BaseModel):
    """管理员列表参数"""

    username: Union[str, None] = Query(default=None)  # 账号
    nickname: Union[str, None] = Query(default=None)  # 昵称
    role: Union[int, None, EmptyStrToNone] = Query(default=None)  # 角色ID


class SystemAuthAdminDetailInSchema(BaseModel):
    """管理员详情参数"""

    id: int = Query(gt=0)  # 主键


class SystemAuthAdminCreateInSchema(BaseModel):
    """管理员新增参数"""

    role_ids: List[int] = Field(alias="roleIds")  # 角色ID
    dept_ids: List[int] = Field(alias="deptIds")  # 部门ID
    post_ids: List[int] = Field(alias="postIds")  # 岗位ID
    username: str = Field(min_length=2, max_length=20)  # 账号
    nickname: str = Field(min_length=2, max_length=30)  # 昵称
    password: str  # 密码
    avatar: str  # 头像
    sort: int = Field(ge=0)  # 排序
    is_disable: int = Field(alias="isDisable", ge=0, le=1)  # 是否禁用: [0=否, 1=是]
    is_multipoint: int = Field(
        alias="isMultipoint", ge=0, le=1
    )  # 多端登录: [0=否, 1=是]


class SystemAuthAdminEditInSchema(SystemAuthAdminCreateInSchema):
    """管理员编辑参数"""

    id: int = Field(gt=0)  # 主键


class SystemAuthAdminUpdateInSchema(BaseModel):
    """管理员更新参数"""

    avatar: str  # 头像
    nickname: str = Field(min_length=2, max_length=30)  # 昵称
    password: str | None = None  # 要修改的密码
    curr_password: str | None = Field(None, min_length=6, max_length=32)  # 当前密码


class SystemAuthAdminDelInSchema(BaseModel):
    """管理员删除参数"""

    id: int = Field(gt=0)  # 主键


class SystemAuthAdminDisableInSchema(BaseModel):
    """管理员状态切换参数"""

    id: int = Field(gt=0)  # 主键


class SystemAuthAdminOutSchema(BaseModel):
    """管理员返回信息"""

    id: int  # 主键
    username: str  # 账号
    nickname: str  # 昵称
    avatar: str  # 头像
    role: Union[str, None] = None  # 角色
    dept: Union[str, None] = None  # 部门
    post: Union[str, None] = None  # 部门
    is_multipoint: int  # 多端登录: [0=否, 1=是]
    is_disable: int  # 是否禁用: [0=否, 1=是]
    last_login_ip: str  # 最后登录IP
    last_login_time: datetime  # 最后登录时间
    created_time: datetime | None = Field(None)  # 创建时间
    updated_time: datetime | None = Field(None)  # 更新时间
    is_delete: int

    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemAuthAdminDetailOutSchema(BaseModel):
    """管理员详情返回信息"""

    id: int  # 主键
    username: str  # 账号
    nickname: str  # 昵称
    avatar: str  # 头像
    role_ids: Union[List[int], str] = Field()  # 角色
    dept_ids: Union[List[int], str] = Field()  # 部门
    post_ids: Union[List[int], str] = Field()  # 岗位
    is_multipoint: int = Field()  # 多端登录: [0=否, 1=是]
    is_disable: int = Field()  # 是否禁用: [0=否, 1=是]
    last_login_ip: str  # 最后登录IP
    last_login_time: datetime = Field()  # 最后登录时间
    created_time: datetime | None = Field(None)  # 创建时间
    updated_time: datetime | None = Field(None)  # 更新时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemAuthAdminSelfOneOutSchema(BaseModel):
    """当前管理员返回部分信息"""

    id: int  # 主键
    username: str  # 账号
    nickname: str  # 昵称
    avatar: str  # 头像
    # role: str  # 角色
    # dept: str = Field(alias='dept_id')  # 部门
    is_multipoint: int = Field()  # 多端登录: [0=否, 1=是]
    is_disable: int = Field()  # 是否禁用: [0=否, 1=是]
    last_login_ip: str = Field()  # 最后登录IP
    last_login_time: datetime = Field()  # 最后登录时间
    created_time: datetime | None = Field(None)  # 创建时间
    updated_time: datetime | None = Field(None)  # 更新时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemAuthAdminSelfOutSchema(BaseModel):
    """当前系统管理员返回信息"""

    user: SystemAuthAdminSelfOneOutSchema  # 用户信息
    permissions: List[str]  # 权限集合: [[*]=>所有权限, ['article:add']=>部分权限]


class SystemAuthRoleDetailInSchema(BaseModel):
    """角色详情参数"""

    id: int = Query(gt=0)  # 主键


class SystemAuthRoleCreateInSchema(BaseModel):
    """新增角色参数"""

    name: str = Field(min_length=1, max_length=30)  # 角色名称
    sort: int = Field(ge=0)  # 角色排序
    is_disable: int = Field(ge=0, le=1)  # 是否禁用: [0=否, 1=是]
    remark: str = Field(default="", max_length=200)  # 角色备注
    menu_ids: Union[str, None] = Field(None)  # 关联菜单
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemAuthRoleEditInSchema(SystemAuthRoleCreateInSchema):
    """编辑角色参数"""

    id: int = Field(gt=0)  # 主键


class SystemAuthRoleDelInSchema(BaseModel):
    """删除角色参数"""

    id: int = Field(gt=0)  # 主键


class SystemAuthRoleOutSchema(BaseModel):
    """系统角色返回信息"""

    id: int  # 主键
    name: str  # 角色名称
    created_time: datetime | None = Field(None)  # 创建时间
    updated_time: datetime | None = Field(None)  # 更新时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemAuthRoleSchema(BaseModel):
    """系统角色返回信息"""

    id: int  # 主键
    name: str = Field(min_length=1, max_length=30)  # 角色名称
    sort: int = Field(ge=0)  # 角色排序
    is_disable: int = Field(ge=0, le=1)  # 是否禁用: [0=否, 1=是]
    remark: str = Field(default="", max_length=200)  # 角色备注
    created_time: datetime | None = Field(None)  # 创建时间
    updated_time: datetime | None = Field(None)  # 更新时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemAuthRoleDetailOutSchema(SystemAuthRoleOutSchema):
    """系统角色返回信息"""

    remark: str  # 角色备注
    menus: List[int] = Field(default_factory=list)  # 关联菜单
    member: int = Field(default=0)  # 成员数量
    sort: int  # 角色排序
    isDisable: int = Field(alias="is_disable")  # 是否禁用: [0=否, 1=是]


class SystemAuthMenuSchema(BaseModel):
    id: int  # 主键
    pid: int  # 上级菜单
    menu_type: str  # 权限类型: [M=目录, C=菜单, A=按钮]
    menu_name: str  # 菜单名称
    menu_icon: str  # 菜单图标
    menu_sort: int  # 菜单排序
    perms: str  # 权限标识
    paths: str  # 路由地址
    component: str  # 前端组件
    selected: str  # 选中路径
    params: str  # 路由参数
    is_cache: int  # 是否缓存: [0=否, 1=是]
    is_show: int  # 是否显示: [0=否, 1=是]
    is_disable: int  # 是否禁用建时间
    created_time: datetime | None = Field(None)  # 创建时间
    updated_time: datetime | None = Field(None)  # 更新时间

    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemAuthMenuDetailInSchema(BaseModel):
    """菜单详情参数"""

    id: int = Query(gt=0)  # 主键


class SystemAuthMenuCreateInSchema(BaseModel):
    """新增菜单参数"""

    pid: int = Field(ge=0)  # 上级菜单
    menu_type: Literal["M", "C", "A"] = Field(
        alias="menuType"
    )  # 权限类型: [M=目录, C=菜单, A=按钮]
    menu_name: str = Field(min_length=1, max_length=30)  # 菜单名称
    menu_icon: Union[str, None] = Field(max_length=100)  # 菜单图标
    menu_sort: int = Field(alias="menuSort", ge=0)  # 菜单排序
    perms: Union[str, None] = Field(max_length=100)  # 权限标识
    paths: Union[str, None] = Field(max_length=200)  # 路由地址
    component: Union[str, None] = Field(max_length=200)  # 前端组件
    selected: Union[str, None] = Field(max_length=200)  # 选中路径
    params: Union[str, None] = Field(max_length=200)  # 路由参数
    is_cache: int = Field(ge=0, le=1)  # 是否缓存: [0=否, 1=是]
    is_show: int = Field(ge=0, le=1)  # 是否显示: [0=否, 1=是]
    is_disable: int = Field(ge=0, le=1)  # 是否禁用: [0=否, 1=是]
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemAuthMenuEditInSchema(SystemAuthMenuCreateInSchema):
    """编辑菜单参数"""

    id: int = Field(gt=0)  # 主键


class SystemAuthMenuDelInSchema(BaseModel):
    """删除菜单参数"""

    id: int = Field(gt=0)  # 主键


class SystemAuthMenuOutSchema(BaseModel):
    """系统菜单返回信息"""

    id: int  # 主键
    pid: int  # 上级菜单
    menu_type: str  # 权限类型: [M=目录, C=菜单, A=按钮]
    menu_name: str  # 菜单名称
    menu_icon: str  # 菜单图标
    menu_sort: int  # 菜单排序
    perms: str  # 权限标识
    paths: str  # 路由地址
    component: str  # 前端组件
    selected: str  # 选中路径
    params: str  # 路由参数
    is_cache: int  # 是否缓存: [0=否, 1=是]
    is_show: int  # 是否显示: [0=否, 1=是]
    is_disable: int  # 是否禁用: [0=否, 1=是]
    created_time: datetime | None = Field(None)  # 创建时间
    updated_time: datetime | None = Field(None)  # 更新时间
    children: Union["SystemAuthMenuOutSchema", None] = None  # 子集
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemAuthPostOutSchema(BaseModel):
    """
    系统岗位返回信息
    """

    id: int
    code: str
    name: str
    remarks: str
    sort: int
    is_stop: int = Field()
    created_time: datetime | None = Field(None)  # 创建时间
    updated_time: datetime | None = Field(None)  # 更新时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemAuthPostAddInSchema(BaseModel):
    """
    系统岗位新增参数
    """

    code: str
    name: str
    remarks: str
    sort: int
    is_stop: int = Field(alias="isStop")


class SystemAuthPostEditInSchema(SystemAuthPostAddInSchema):
    """
    系统岗位编辑参数
    """

    id: int = Field(gt=0)  # 主键


class SystemAuthPostDelInSchema(BaseModel):
    """
    系统岗位删除参数
    """

    id: int = Field(gt=0)  # 主键


class SystemAuthPostDetailInSchema(BaseModel):
    """
    系统岗位详情参数
    """

    id: int = Query(gt=0)  # 主键


class SystemAuthDeptOutSchema(BaseModel):
    """
    系统部门返回信息
    """

    id: int
    pid: int  # 上级ID
    name: str  # 部门名称
    duty: str  # 负责人名
    mobile: str  # 联系电话
    sort: int  # 部门排序
    is_stop: int = Field()
    created_time: datetime | None = Field(None)  # 创建时间
    updated_time: datetime | None = Field(None)  # 更新时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


# class SystemAuthPostOutSchema(BaseModel):
#     id: int
#     pid: int  # 上级ID
#     name: str  # 部门名称
#     duty: str  # 负责人名
#     mobile: str  # 联系电话
#     sort: int  # 部门排序
#     is_stop: int = Field()
#     created_time: datetime | None = Field(None)  # 创建时间
#     updated_time: datetime | None = Field(None)  # 更新时间
#     model_config = {
#         "alias_generator": ToolsUtil.to_lower_camel,
#         "populate_by_name": True,
#     }


class SystemAuthDeptDetailInSchema(BaseModel):
    id: int = Query(gt=0)  # 主键


class SystemAuthDeptDeleteInSchema(BaseModel):
    id: int = Query(gt=0)  # 主键


class SystemAuthDeptAddInSchema(BaseModel):
    pid: int  # 上级ID
    name: str  # 部门名称
    duty: str  # 负责人名
    mobile: str  # 联系电话
    sort: int  # 部门排序
    is_stop: int = Field(alias="isStop")


class SystemAuthDeptListInSchema(BaseModel):
    is_stop: Union[int, None, EmptyStrToNone] = Query(default=None)
    name: Union[str, None] = Query(default=None)
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemAuthDeptEditInSchema(SystemAuthDeptAddInSchema):
    id: int = Query(gt=0)  # 主键


class SystemLogOperateInSchema(BaseModel):
    """操作日志列表参数"""

    title: Union[str, None] = Query(default=None)  # 操作标题
    username: Union[str, None] = Query(default=None)  # 用户账号
    ip: Union[str, None] = Query(default=None)  # 请求IP
    type: Union[str, None] = Query(default=None)  # 请求类型: GET/POST/PUT
    status: Union[int, None, EmptyStrToNone] = Query(
        default=None
    )  # 执行状态: [1=成功, 2=失败]
    url: Union[str, None] = Query(default=None)  # 请求地址
    start_time: Union[date, None, EmptyStrToNone] = Query(None)  # 开始时间
    end_time: Union[date, None, EmptyStrToNone] = Query(None)  # 结束时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemLogOperateOutSchema(BaseModel):
    """操作日志返回信息"""

    id: int  # 主键
    username: str  # 用户账号
    nickname: str  # 用户昵称
    type: str  # 请求类型: GET/POST/PUT
    title: str  # 操作标题
    method: str  # 请求方式
    ip: str  # 请求IP
    url: str  # 请求地址
    args: str  # 请求参数
    error: str  # 错误信息
    status: int  # 执行状态: [1=成功, 2=失败]
    task_time: int | None = Field(None)  # 执行耗时
    start_time: datetime = Field()  # 开始时间
    end_time: datetime = Field()  # 结束时间
    created_time: datetime | None = Field(None)  # 创建时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemLogLoginInSchema(BaseModel):
    """登录日志列表参数"""

    username: Union[str, None] = Query(default=None)  # 登录账号
    status: Union[int, None, EmptyStrToNone] = Query(
        default=None
    )  # 操作状态: [1=成功, 2=失败]
    start_time: Union[date, None, EmptyStrToNone] = Query(alias="startTime")  # 开始时间
    end_time: Union[date, None, EmptyStrToNone] = Query(alias="endTime")  # 结束时间


class SystemLogLoginOutSchema(BaseModel):
    """登录日志返回信息"""

    id: int  # 主键
    username: str  # 登录账号
    ip: str  # 来源IP
    os: str  # 操作系统
    browser: str  # 浏览器
    status: int  # 操作状态: [1=成功, 2=失败]
    created_time: datetime | None = Field(None)  # 创建时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SystemConfigSchema(BaseModel):
    """
    系统配置
    """

    id: int
    type: str
    name: str
    value: str | None = None
    created_time: datetime | None = Field(None)
    updated_time: datetime | None = Field(None)
